<?php

class Dictionary_model extends MY_CI_Model
{

    /**
     * @access public
     */
    public function __construct()
    {
        $this->table        = 'dictionary';
        $this->primary_id   = 'id';
        $this->primary_name = 'code';

        parent::__construct();

    }

    /**
     * 查询字典表中的选项，如果当前医院为空，则返回公用的字典配置选项，如果不为空，则返回当前医院配置的
     * @param string $code
     * @param string $fields
     * @param string $hospital_id
     * @param int $obj_type 0:app,1:微信
     * @return array
     */
    public function get_dictionary_by_code($code = '', $fields = "*", $hospital_id = '', $obj_type = 0)
    {
        $hospital_id = $hospital_id == '' ? $_SESSION['hospital_id'] : $hospital_id;
        $fields      = $fields . ',' . 'hospital_id';
        $this->db->select($fields);
        $this->db->from('dictionary');
        $this->db->where('active', 1);
        $this->db->where('is_delete', 0);
        $this->db->where('parent_id', $code);
        $this->db->where('obj_type', $obj_type);
        $this->db->where_in('hospital_id', [$hospital_id, 0]);
        $this->db->order_by('remarks', 'asc');
        $items      = $this->db->get()->result_array();
        $this_arr   = [];
        $common_arr = [];
        foreach ($items as $k => $v) {
            if ($v['hospital_id'] == 0) {
                $common_arr[] = $v;
            } else {
                $this_arr[] = $v;
            }
        }
        if (!empty($this_arr)) {
            return $this_arr;
        } else {
            return $common_arr;
        }
    }

    /**
     * 格式化输出字典信息[code=>'']
     * @param string $code
     * @param string $fields
     * @param string $hospital_id
     * @param int $obj_type 0:app,1:微信
     * @return array
     */
    public function formate_dictionary_by_code($code = '', $fields = "*", $hospital_id = '', $obj_type = 0)
    {
        $list        = $this->get_dictionary_by_code($code, $fields, $hospital_id, $obj_type);
        $formate_arr = [];
        foreach ($list as $k => $v) {
            $formate_arr[$v['code']] = $v;
        }
        return $formate_arr;
    }

}

?>